package com.webshh.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;

/**
 * @author frank
 * @date 2019-02-25 17:44
 * @desc
 **/
public class IoReader extends Thread {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private boolean stop;
    private ShellSocket shell;
    private InputStream in;

    public IoReader(ShellSocket shell, String name) {
        super(name);
        this.shell = shell;
        try {
            in = shell.getInputStream();
        } catch (Exception e) {
            logger.error("实例化Reader发生错误！{}", new Object[]{e.getMessage()});
            destory();
        }
        this.start();
    }

    @Override
    public void run() {
        while (!stop) {
            try {
                byte[] bytes = new byte[1024];
                in.read(bytes);
                shell.write(bytes);
            } catch (Exception e) {
                logger.error("读取输入流发生错误！{}", new Object[]{e.getMessage()});
                destory();
            }
        }
    }

    public void destory() {
        this.stop = true;
    }

}
